<?php

namespace App\Traits\Relation\Inspection;

use App\Models\Inspection;
use App\Models\Inspection\Line;
use App\Models\Inspection\Node;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

/**
 * 定义圈信息关联关系
 *
 * Trait CircleRelation
 * @package App\Traits\Relation\Inspection
 * Date: 22/04/2018
 * @author George
 */
trait CircleRelation
{
    /**
     * 获取圈所属线路信息
     *
     * Date: 22/04/2018
     * @author George
     * @return BelongsTo
     */
    public function line()
    {
        return $this->belongsTo(Line::class, 'line_id', 'id');
    }

    /**
     * 获取圈的节点信息
     *
     * Date: 22/04/2018
     * @author George
     * @return BelongsToMany
     */
    public function nodes()
    {
        return $this->belongsToMany(Node::class, 'circle_node_pivot', 'circle_id', 'node_id', 'id', 'id')->withPivot('time');
    }

	/**
	 * 获取圈的所有巡检记录
	 *
	 * Date: 19/05/2018
	 * @author George
	 * @return HasMany
	 */
	public function inspections()
	{
		return $this->hasMany(Inspection::class, 'circle_id', 'id')->whereDate('inspections.created_at', date('Y-m-d'));
    }
}
